import time
from datetime import datetime

import requests

from com.yyp.app.pusher.pusher_wx import Pusher

cookies = {
    'JSESSIONID': 'C53F1DC164290AE3172E31637405E1BF',
    'https_waf_cookie': 'c253f474-358a-48016d27c1934cc50b5082bdfe2853c793b0',
}

headers = {
    'host': 'mzfwh.huashan.org.cn',
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh-Hans;q=0.9',
    'x-requested-with': 'XMLHttpRequest',
    'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_1_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.31(0x18001f2f) NetType/WIFI Language/zh_CN',
    'referer': 'https://mzfwh.huashan.org.cn/doctor/detail?id=14464&hospitalid=17',
    # 'accept-encoding': 'gzip, deflate, br',
    # 'cookie': 'JSESSIONID=C53F1DC164290AE3172E31637405E1BF; https_waf_cookie=c253f474-358a-48016d27c1934cc50b5082bdfe2853c793b0',
}

params = [
    {
        # 陆小年
        'cmd': 'DoctorDetailMerge',
        'doctorid': '14464',
        'md5ParamNames': 'doctorid',
    }, {
        # 韩凌
        'cmd': 'DoctorDetailMerge',
        'doctorid': '29232',
        'md5ParamNames': 'doctorid',
    },
    {
        # 李巍
        'cmd': 'DoctorDetailMerge',
        'doctorid': '18323',
        'md5ParamNames': 'doctorid',
    },
    {
        # 方栩
        'cmd': 'DoctorDetailMerge',
        'doctorid': '4496',
        'md5ParamNames': 'doctorid',
    }
]

stop = True
try:
    while stop:
        for param in params:
            if not stop:
                break
            time.sleep(1)
            requests.packages.urllib3.disable_warnings()
            response = requests.get('https://mzfwh.huashan.org.cn/common/api', params=param, cookies=cookies,
                                    headers=headers,
                                    verify=False)
            if response.json()["code"] == 200:
                # print(response.json()["name"])
                # print(response.json()["cliniclist"]["总院"])
                if response.json()["cliniclist"]["总院"].get("专家门诊"):
                    for item in response.json()["cliniclist"]["总院"].get("专家门诊"):
                        if item["statustext"] == "预约" and datetime.strptime(item["date"],
                                                                            '%Y-%m-%d') < datetime.strptime(
                                '2023-01-10', '%Y-%m-%d'):
                            content = str(response.json()["name"]) + " : " + str(
                                datetime.strptime(item["date"], '%Y-%m-%d')) + " : " + str(item)
                            print(content)
                            pusher = Pusher("预约通知", content)
                            pusher.pusher_wx_msg()
                            # stop = True
                            stop = False
                            break

except Exception as e:
    print('except:', e)
finally:
    stop = True
